package org.jgs1904.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.jgs1904.bean.ConditionBean;
import org.jgs1904.bean.Pager;
import org.jgs1904.dao.PostDao;
import org.jgs1904.entity.Post;
import org.jgs1904.util.ConditionUtil;
import org.jgs1904.util.DataSourceUtil;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 帖子数据操作类
 *
 * @author junki
 * @date 2020年3月26日
 */
public class PostDaoImpl implements PostDao {


    @Override
    public List<Post> selectList() throws SQLException {

        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        String sql = "select post.*,user.nickname,count(comment.id) as comment_count from post left join comment on post.id = comment.post_id, user where post.user_id = user.id group by post.id order by post.create_time desc";

        List<Post> result = qr.query(sql, new BeanListHandler<>(Post.class));

        return result;
    }

    @Override
    public Post insert(Post post) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        String sql = "insert into post(title,type,summary,content,user_id,create_time,image) value(?,?,?,?,?,?,?) ";

        BigInteger id = (BigInteger) qr.insert(sql, new ScalarHandler<>(1), post.getTitle(), post.getType(),
                post.getSummary(), post.getContent(), post.getUser_id(), post.getCreate_time(), post.getImage());

        post.setId(id.longValue());

        return post;
    }

    @Override
    public Post selectOneById(Long id) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        String sql = "select post.*,user.nickname from post,user where post.user_id = user.id and post.id = ?";

        Post result = qr.query(sql, new BeanHandler<>(Post.class), id);

        return result;
    }

    @Override
    public Pager<Post> selectPage(Long currentPage, Long pageSize, Long pageNumShowLength, Map<String, String[]> paramMap) throws SQLException {

        // 获取总数据量
        Long totalRows = count(paramMap);
        // 初始化pager对象
        Pager<Post> pager = new Pager<>(currentPage, pageSize, totalRows, pageNumShowLength);

        // 分页查询
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        ConditionBean condition = ConditionUtil.getCondition(paramMap, Post.class, "post");

        String sql = "select post.*,user.nickname,count(comment.id) as comment_count "
                + "from post left join comment on post.id = comment.post_id, user where post.user_id = user.id "
                + condition.getSql()
                + "group by post.id order by post.create_time desc limit ?,?";

        List<Object> params = condition.getParams();
        params.add(pager.getStartIndex());
        params.add(pager.getPageSize());

        List<Post> postList = qr.query(sql, new BeanListHandler<>(Post.class), params.toArray());

        // 将数据封装到pager对象
        pager.setDataList(postList);

        return pager;
    }

    @Override
    public Long count(Map<String, String[]> paramMap) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        ConditionBean condition = ConditionUtil.getCondition(paramMap, Post.class, "post");

        String sql = "select count(*) from post, user where post.user_id = user.id " + condition.getSql();

        Long count = (Long) qr.query(sql, new ScalarHandler<>(1), condition.getParams().toArray());

        return count;

    }

}
